package com.openbravo.pos.printer;

import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.printer.escpos.CodesEpson;
import com.openbravo.pos.printer.escpos.CodesIthaca;
import com.openbravo.pos.printer.escpos.CodesStar;
import com.openbravo.pos.printer.escpos.CodesSurePOS;
import com.openbravo.pos.printer.escpos.CodesTMU220;
import com.openbravo.pos.printer.escpos.DeviceDisplayESCPOS;
import com.openbravo.pos.printer.escpos.DeviceDisplaySurePOS;
import com.openbravo.pos.printer.escpos.DevicePrinterESCPOS;
import com.openbravo.pos.printer.escpos.DevicePrinterPlain;
import com.openbravo.pos.printer.escpos.PrinterWritter;
import com.openbravo.pos.printer.escpos.PrinterWritterFile;
import com.openbravo.pos.printer.escpos.PrinterWritterRXTX;
import com.openbravo.pos.printer.escpos.UnicodeTranslatorEur;
import com.openbravo.pos.printer.escpos.UnicodeTranslatorInt;
import com.openbravo.pos.printer.escpos.UnicodeTranslatorStar;
import com.openbravo.pos.printer.escpos.UnicodeTranslatorSurePOS;
import com.openbravo.pos.printer.javapos.DeviceDisplayJavaPOS;
import com.openbravo.pos.printer.javapos.DeviceFiscalPrinterJavaPOS;
import com.openbravo.pos.printer.javapos.DevicePrinterJavaPOS;
import com.openbravo.pos.printer.printer.DevicePrinterPrinter;
import com.openbravo.pos.printer.screen.DeviceDisplayPanel;
import com.openbravo.pos.printer.screen.DeviceDisplayWindow;
import com.openbravo.pos.printer.screen.DevicePrinterPanel;
import com.openbravo.pos.util.StringParser;
import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DeviceTicket
{
  private static Logger logger = Logger.getLogger("com.openbravo.pos.printer.DeviceTicket");
  private DeviceFiscalPrinter m_deviceFiscal;
  private DeviceDisplay m_devicedisplay;
  private DevicePrinter m_nullprinter;
  private Map<String, DevicePrinter> m_deviceprinters;
  private List<DevicePrinter> m_deviceprinterslist;

  public DeviceTicket()
  {
    this.m_deviceFiscal = new DeviceFiscalPrinterNull();
    this.m_devicedisplay = new DeviceDisplayNull();
    this.m_nullprinter = new DevicePrinterNull();
    this.m_deviceprinters = new HashMap();
    this.m_deviceprinterslist = new ArrayList();
    DevicePrinterPanel localDevicePrinterPanel = new DevicePrinterPanel();
    this.m_deviceprinters.put("1", localDevicePrinterPanel);
    this.m_deviceprinterslist.add(localDevicePrinterPanel);
  }

  public DeviceTicket(Component paramComponent, AppProperties paramAppProperties)
  {
    PrinterWritterPool localPrinterWritterPool = new PrinterWritterPool();
    StringParser localStringParser1 = new StringParser(paramAppProperties.getProperty("machine.fiscalprinter"));
    String str1 = localStringParser1.nextToken(':');
    String str2 = localStringParser1.nextToken(',');
    try
    {
      if ("javapos".equals(str1))
        this.m_deviceFiscal = new DeviceFiscalPrinterJavaPOS(str2);
      else
        this.m_deviceFiscal = new DeviceFiscalPrinterNull();
    }
    catch (TicketPrinterException localTicketPrinterException1)
    {
      this.m_deviceFiscal = new DeviceFiscalPrinterNull(localTicketPrinterException1.getMessage());
    }
    StringParser localStringParser2 = new StringParser(paramAppProperties.getProperty("machine.display"));
    String str3 = localStringParser2.nextToken(':');
    String str4 = localStringParser2.nextToken(',');
    String str5 = localStringParser2.nextToken(',');
    if (("serial".equals(str3)) || ("rxtx".equals(str3)) || ("file".equals(str3)))
    {
      str5 = str4;
      str4 = str3;
      str3 = "epson";
    }
    try
    {
      if ("screen".equals(str3))
        this.m_devicedisplay = new DeviceDisplayPanel();
      else if ("window".equals(str3))
        this.m_devicedisplay = new DeviceDisplayWindow();
      else if ("epson".equals(str3))
        this.m_devicedisplay = new DeviceDisplayESCPOS(localPrinterWritterPool.getPrinterWritter(str4, str5), new UnicodeTranslatorInt());
      else if ("surepos".equals(str3))
        this.m_devicedisplay = new DeviceDisplaySurePOS(localPrinterWritterPool.getPrinterWritter(str4, str5));
      else if ("ld200".equals(str3))
        this.m_devicedisplay = new DeviceDisplayESCPOS(localPrinterWritterPool.getPrinterWritter(str4, str5), new UnicodeTranslatorEur());
      else if ("javapos".equals(str3))
        this.m_devicedisplay = new DeviceDisplayJavaPOS(str4);
      else
        this.m_devicedisplay = new DeviceDisplayNull();
    }
    catch (TicketPrinterException localTicketPrinterException2)
    {
      logger.log(Level.WARNING, localTicketPrinterException2.getMessage(), localTicketPrinterException2);
      this.m_devicedisplay = new DeviceDisplayNull(localTicketPrinterException2.getMessage());
    }
    this.m_nullprinter = new DevicePrinterNull();
    this.m_deviceprinters = new HashMap();
    this.m_deviceprinterslist = new ArrayList();
    int i = 1;
    String str6 = Integer.toString(i);
    for (String str7 = paramAppProperties.getProperty("machine.printer"); (str7 != null) && (!"".equals(str7)); str7 = paramAppProperties.getProperty("machine.printer." + str6))
    {
      StringParser localStringParser3 = new StringParser(str7);
      String str8 = localStringParser3.nextToken(':');
      String str9 = localStringParser3.nextToken(',');
      String str10 = localStringParser3.nextToken(',');
      if (("serial".equals(str8)) || ("rxtx".equals(str8)) || ("file".equals(str8)))
      {
        str10 = str9;
        str9 = str8;
        str8 = "epson";
      }
      try
      {
        if ("screen".equals(str8))
        {
          addPrinter(str6, new DevicePrinterPanel());
        }
        else if ("printer".equals(str8))
        {
          if ((str10 == null) || (str10.equals("")) || (str10.equals("true")))
            str10 = "receipt";
          else if (str10.equals("false"))
            str10 = "standard";
          addPrinter(str6, new DevicePrinterPrinter(paramComponent, str9, Integer.parseInt(paramAppProperties.getProperty("paper." + str10 + ".x")), Integer.parseInt(paramAppProperties.getProperty("paper." + str10 + ".y")), Integer.parseInt(paramAppProperties.getProperty("paper." + str10 + ".width")), Integer.parseInt(paramAppProperties.getProperty("paper." + str10 + ".height")), paramAppProperties.getProperty("paper." + str10 + ".mediasizename")));
        }
        else if ("epson".equals(str8))
        {
          addPrinter(str6, new DevicePrinterESCPOS(localPrinterWritterPool.getPrinterWritter(str9, str10), new CodesEpson(), new UnicodeTranslatorInt()));
        }
        else if ("tmu220".equals(str8))
        {
          addPrinter(str6, new DevicePrinterESCPOS(localPrinterWritterPool.getPrinterWritter(str9, str10), new CodesTMU220(), new UnicodeTranslatorInt()));
        }
        else if ("star".equals(str8))
        {
          addPrinter(str6, new DevicePrinterESCPOS(localPrinterWritterPool.getPrinterWritter(str9, str10), new CodesStar(), new UnicodeTranslatorStar()));
        }
        else if ("ithaca".equals(str8))
        {
          addPrinter(str6, new DevicePrinterESCPOS(localPrinterWritterPool.getPrinterWritter(str9, str10), new CodesIthaca(), new UnicodeTranslatorInt()));
        }
        else if ("surepos".equals(str8))
        {
          addPrinter(str6, new DevicePrinterESCPOS(localPrinterWritterPool.getPrinterWritter(str9, str10), new CodesSurePOS(), new UnicodeTranslatorSurePOS()));
        }
        else if ("plain".equals(str8))
        {
          addPrinter(str6, new DevicePrinterPlain(localPrinterWritterPool.getPrinterWritter(str9, str10)));
        }
        else if ("javapos".equals(str8))
        {
          addPrinter(str6, new DevicePrinterJavaPOS(str9, str10));
        }
      }
      catch (TicketPrinterException localTicketPrinterException3)
      {
        logger.log(Level.WARNING, localTicketPrinterException3.getMessage(), localTicketPrinterException3);
      }
      i++;
      str6 = Integer.toString(i);
    }
  }

  private void addPrinter(String paramString, DevicePrinter paramDevicePrinter)
  {
    this.m_deviceprinters.put(paramString, paramDevicePrinter);
    this.m_deviceprinterslist.add(paramDevicePrinter);
  }

  public DeviceFiscalPrinter getFiscalPrinter()
  {
    return this.m_deviceFiscal;
  }

  public DeviceDisplay getDeviceDisplay()
  {
    return this.m_devicedisplay;
  }

  public DevicePrinter getDevicePrinter(String paramString)
  {
    DevicePrinter localDevicePrinter = (DevicePrinter)this.m_deviceprinters.get(paramString);
    return localDevicePrinter == null ? this.m_nullprinter : localDevicePrinter;
  }

  public List<DevicePrinter> getDevicePrinterAll()
  {
    return this.m_deviceprinterslist;
  }

  public static String getWhiteString(int paramInt, char paramChar)
  {
    char[] arrayOfChar = new char[paramInt];
    for (int i = 0; i < paramInt; i++)
      arrayOfChar[i] = paramChar;
    return new String(arrayOfChar);
  }

  public static String getWhiteString(int paramInt)
  {
    return getWhiteString(paramInt, ' ');
  }

  public static String alignBarCode(String paramString, int paramInt)
  {
    if (paramString.length() > paramInt)
      return paramString.substring(paramString.length() - paramInt);
    return getWhiteString(paramInt - paramString.length(), '0') + paramString;
  }

  public static String alignLeft(String paramString, int paramInt)
  {
    if (paramString.length() > paramInt)
      return paramString.substring(0, paramInt);
    return paramString + getWhiteString(paramInt - paramString.length());
  }

  public static String alignRight(String paramString, int paramInt)
  {
    if (paramString.length() > paramInt)
      return paramString.substring(paramString.length() - paramInt);
    return getWhiteString(paramInt - paramString.length()) + paramString;
  }

  public static String alignCenter(String paramString, int paramInt)
  {
    if (paramString.length() > paramInt)
      return alignRight(paramString.substring(0, (paramString.length() + paramInt) / 2), paramInt);
    return alignRight(paramString + getWhiteString((paramInt - paramString.length()) / 2), paramInt);
  }

  public static String alignCenter(String paramString)
  {
    return alignCenter(paramString, 42);
  }

  public static final byte[] transNumber(String paramString)
  {
    if (paramString == null)
      return null;
    byte[] arrayOfByte = new byte[paramString.length()];
    for (int i = 0; i < paramString.length(); i++)
      arrayOfByte[i] = transNumberChar(paramString.charAt(i));
    return arrayOfByte;
  }

  public static byte transNumberChar(char paramChar)
  {
    switch (paramChar)
    {
    case '0':
      return 48;
    case '1':
      return 49;
    case '2':
      return 50;
    case '3':
      return 51;
    case '4':
      return 52;
    case '5':
      return 53;
    case '6':
      return 54;
    case '7':
      return 55;
    case '8':
      return 56;
    case '9':
      return 57;
    }
    return 48;
  }

  private static class PrinterWritterPool
  {
    private Map<String, PrinterWritter> m_apool = new HashMap();

    public PrinterWritter getPrinterWritter(String paramString1, String paramString2)
      throws TicketPrinterException
    {
      String str = paramString1 + "-->" + paramString2;
      Object localObject = (PrinterWritter)this.m_apool.get(str);
      if (localObject == null)
        if (("serial".equals(paramString1)) || ("rxtx".equals(paramString1)))
        {
          localObject = new PrinterWritterRXTX(paramString2);
          this.m_apool.put(str, (PrinterWritter) localObject);
        }
        else if ("file".equals(paramString1))
        {
          localObject = new PrinterWritterFile(paramString2);
          this.m_apool.put(str, (PrinterWritter) localObject);
        }
        else
        {
          throw new TicketPrinterException();
        }
      return (PrinterWritter)localObject;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.printer.DeviceTicket
 * JD-Core Version:    0.6.0
 */